/**
 * Created by wangshaohua on 2018/2/7.
 */
moduleLearningMap.controller('LanguageMgt', function ($state, ConstantService, LanguageService, GeneralResource, $$CambioConfig, $scope, $log, LoadingService, DialogService, LanguageResource, UploadFileService) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('语言详细'));
        $scope.projectNameOptions = Enums.LanguageProjectNameEnums.options;
        $scope.projectName = null;
    });

    $scope.upload = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'FILE', $scope).then(function (o) {
                $scope.currentLanguage.file = o.id;
                $scope.currentLanguage.fileName = o.originalName;
            });
        }
    };

    $scope.download = function (record) {
        if (!StringUtils.isNlOrUndOrEmpty(record.file)) {
            window.open($$CambioConfig.DOWNLOAD_URL.format({id: record.file}), '_blank');
        }
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    ///upload/{projectName}/{locale}/{file}
    $scope.submit = function () {
        //LoadingService.show();
        $scope.currentLanguage.projectName = $scope.currentLanguage.projectNameOption.value;
        LanguageResource.upload({}, $scope.currentLanguage, function (htmlResult) {
            // $log.debug("h5项目更新结果", htmlResult);
            angular.copy(htmlResult, $scope.currentHtml);
            DialogService.close();
            Alert.success();
            //LoadingService.hide();
        });
    };

    $scope.read = function (workbook) {
        $log.debug(workbook);
        var rawJson = XLS.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]);
        $log.debug("excel数据", rawJson);
        if (!checkLanguageJson(rawJson)) {
            Alert.error(LanguageService.language('数据格式错误！'));
        } else {
            $scope.currentLanguage.languages = jsonToLanguage(rawJson);
            $log.debug("转换后数据", $scope.currentLanguage.languages);
            //$scope.currentQuizTopicGroup.gaugeTopics = topicList;
            $scope.$apply();
        }
    }

    var checkLanguageJson = function (rawJson) {

        return true;
    }

    var jsonToLanguage = function (rawJson) {
        var languageJson = new Array();

        rawJson.forEach(function (record, index) {
            var language = {};
            language.key = record['KEY'];
            language.value = record['VALUE'];
            languageJson.push(language);
        });
        return languageJson;
    }

    $scope.downloadTemplate = function (value) {
        //download
        var params = {
            className: Models.LanguageTemplate.className,
            all: true,
            query: {
                projectName: {
                    field: Models.LanguageTemplate.query.projectName.name,
                    enumClass: Models.LanguageTemplate.query.projectName.enumClass,
                    value: value
                }
            }
        };
        GeneralResource.query({}, params, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            var languageTemplate = queryResult.data[0];
            if (!StringUtils.isNlOrUndOrEmpty(languageTemplate.file)) {
                window.open($$CambioConfig.DOWNLOAD_URL.format({id: languageTemplate.file}), '_blank');
            }
            //LoadingService.hide();
        });
    }
});